package com.oa.controller;

import com.oa.back.ResponseBack;
import com.oa.common.annotion.Interceptor;
import com.oa.common.base.Pageable;
import com.oa.common.enums.CodeStatus;
import com.oa.common.enums.Gender;
import com.oa.common.enums.UserStatus;
import com.oa.common.eums.EntityStatus;
import com.oa.service.DepartmentService;
import com.oa.service.RoleService;
import com.oa.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/manage/user")
public class UserController extends BaseController {

    /** 日志的输出类 **/
    private static final Logger logger= LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private DepartmentService departmentService;

    @RequestMapping(value = "index.html",method = RequestMethod.GET)
    public String index(ModelMap modelMap, String key, Pageable pageable,String check){
        modelMap.addAttribute("page",userService.findByPage(pageable,key));
        modelMap.addAttribute("pageable",pageable);
        modelMap.addAttribute("check",check);
        modelMap.addAttribute("key",key);
        modelMap.addAttribute("departments",departmentService.findParent());
        return "/admin/user/index";
    }

    @RequestMapping(value = "add.html",method = RequestMethod.GET)
    public String add(ModelMap modelMap){
        modelMap.addAttribute("userStatus",UserStatus.values());
        modelMap.addAttribute("genders",Gender.values());
        return "/admin/user/add";
    }

    @RequestMapping(value = "validateUsername",method = RequestMethod.GET)
    @ResponseBody
    @Interceptor(actionName = "校验用户是否存在")
    public Boolean validateUsername(String username,Long id){
        return !userService.validateUsername(username, id);
    }

    @RequestMapping(value = "validatePhone",method = RequestMethod.GET)
    @ResponseBody
    @Interceptor(actionName = "校验用户手机号码是否存在")
    public Boolean validatePhone(String phone,Long id){
        return !userService.validatePhone(phone, id);
    }

    @RequestMapping(value = "validateEmail",method = RequestMethod.GET)
    @ResponseBody
    @Interceptor(actionName = "校验用户电子邮件的接口")
    public Boolean validateEmail(String email,Long id){
        return !userService.validateEmail(email, id);
    }

    @RequestMapping(value = "submit",method = RequestMethod.POST)
    @ResponseBody
    @Interceptor(actionName = "提交员工的接口")
    public ResponseBack<String> submit(String params){
        try{
            return userService.submit(params);
        }catch (RuntimeException ex){
            logger.error("提交员工接口异常:",ex);
            return ResponseBack.fial(CodeStatus.SYSTEM_FAIL.getResponseStatus());
        }
    }

    @RequestMapping(value = "disimission",method =RequestMethod.POST)
    @ResponseBody
    @Interceptor(actionName = "设置员工离职")
    public ResponseBack<String> disimission(Long id){
        try{
            return userService.dimission(id);
        }catch (RuntimeException ex){
            logger.error("设置员工离职的接口异常:",ex);
            return ResponseBack.fial(CodeStatus.SYSTEM_FAIL.getResponseStatus());
        }
    }
}
